import RPi.GPIO as GPIO
import os
import time

class DS18B20(object):
    
    def __init__(self,str_id):
        self.str_id = str_id
        
    def read_DS18B20(self):
        # 读取温度传感器的数值
        location = os.path.join( "/sys/bus/w1/devices",self.str_id,"w1_slave") 

        if os.path.exists(location):
            with open(location) as tf:
                lines = tf.read().splitlines()
            
            text = lines[-1]
            temperaturedata = text.split(" ")[-1]
            
            temperature = float(temperaturedata[2:])
            
            return temperature/1000.0
            
        else:
            return False
            


if __name__ == "__main__":
    
    str_id = "28-00000065b9be"
    m_ds18b20 =  DS18B20(str_id)
    

    while True:
    
        t = m_ds18b20.read_DS18B20()
        
        if t:
            print("\r温度：%2.2f °C\n"%(t),end="")
        else:
            print("error")
        
        
        time.sleep(0.1)
